﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 图书管理系统
{
    public partial class PwdLoginForm : Form
    {
        public int mode;

        public string Uid;
        public PwdLoginForm()
        {
            InitializeComponent();
        }

        private void Confirmbutton_Click(object sender, EventArgs e)
        {
            string id = this.IdtextBox.Text.Trim();
            string pwd = this.PwdtextBox.Text.Trim();
            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(pwd))
            {
                string strsql = "select * from MemberInfo where uid = \'" + id + "\' and pwd = \'" + pwd + "\'";
                SqlConnection con = Utils.SQLTool.GetCon();
                SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (!(dt.Rows.Count > 0))
                {
                    MessageBox.Show("账号或密码错误！");
                    this.IdtextBox.Text = "";
                    this.PwdtextBox.Text = "";
                }
                else
                {
                    string Strsql = "select power from MemberInfo where uid = \'" + id + "\' and pwd = \'" + pwd + "\'";
                    SqlCommand sqlCommand = con.CreateCommand();
                    sqlCommand.CommandText = Strsql;
                    con.Open();
                    string power = sqlCommand.ExecuteScalar().ToString();

                    //账号密码正确 通过角色进入窗体
                    switch (power)
                    {
                        case "系统管理员":
                            this.mode = 0;
                            this.Uid = id;
                            this.DialogResult = DialogResult.OK;
                            break;
                        case "普通用户":
                            this.mode = 1;
                            this.Uid = id;
                            this.DialogResult = DialogResult.OK;
                            break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                MessageBox.Show("信息为空格或空，请重新登入！");
                this.IdtextBox.Text = "";
                this.PwdtextBox.Text = "";
            }
        }

        private void Returnbutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
